<template>
	<view class="page">
		<view class="btn" @click="openCamera('normal')">
			打开相机拍照
		</view>
		
		<view class="btn" @click="openCamera('scanCode')">
			打开相机扫描二维码
		</view>
	</view>
</template>

<script>
	export default {
		methods: {
			openCamera(mode) {
				const {
					cameraAuthorized,
					microphoneAuthorized,
					osName
				} = uni.getSystemInfoSync()

				if (!cameraAuthorized) {
					uni.showModal({
						title: '提示',
						content: '您关闭了摄像头权限，请允许微信使用摄像头权限',
						confirmText: '去设置',
						success: (res)=> {
							if(res.confirm) {
								uni.openAppAuthorizeSetting()
							}
						}
					})
					return
				}
				// ios 摄像头&麦克风绑定
				if (!microphoneAuthorized && osName === 'ios') {
					uni.showModal({
						title: '提示',
						content: '您关闭了摄像头及麦克风权限，请允许微信使摄像头及麦克风权限',
						confirmText: '去设置',
						success: (res)=> {
							if(res.confirm) {
								uni.openAppAuthorizeSetting()
							}
						}
					})
					return
				}
				uni.navigateTo({
					url:`/pages/camera/camera?mode=${mode}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 60rpx;

		.btn {
			border-radius: 24rpx;
			text-align: center;
			line-height: 92rpx;
			background: #39b077;
			color: #fff;
			margin-bottom: 40rpx;
		}
	}
</style>